﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DCS7GUI;
using WeifenLuo.WinFormsUI.Docking;

namespace DCS7Design
{
    public partial class DCSGUIDesigner : DockContent
    {
        List<IDCSGUI> GUI = new List<IDCSGUI>();
        Bitmap canvas;

        public DCSGUIDesigner()
        {
            InitializeComponent();
            canvas = new Bitmap(96 * 4, 64 * 4);
            GUI.Add(new GUIRLargeWin()
            {
                Sprite = "F800F8F8F8",
                Title = "Large Win",
            });
            Render();
            UpdateTree();
        }

        private void UpdateTree()
        {
            treeView1.Nodes.Clear();
            foreach (IDCSGUI gui in GUI)
            {
                IGUIContainer container = gui as IGUIContainer;
                TreeNode node = new TreeNode(container.GetType().Name);
                node.Tag = container;
                foreach (IDCSGUI gui2 in container.Controls)
                {
                    TreeNode obj = new TreeNode(gui2.GetType().Name);
                    obj.Tag = gui2;
                    node.Nodes.Add(obj);
                }
                treeView1.Nodes.Add(node);
            }
            treeView1.SelectedNode = treeView1.Nodes[0];
        }

        void Render()
        {
            for (int x = 0; x < 96 * 4; x++)
                for (int y = 0; y < 64 * 4; y++)
                    canvas.SetPixel(x, y, Color.White);
            foreach (IDCSGUI item in GUI)
                item.Render(canvas, 0, 0);
            pictureBox1.Image = canvas;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node == null)
                return;
            IDCSGUI gui = (IDCSGUI)e.Node.Tag;
            propertyGrid1.SelectedObject = gui;
            Invalidate(true);
        }

        private void propertyGrid1_Click(object sender, EventArgs e)
        {

        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            Render();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedItem == null)
                return;
            IGUIContainer selectedContainer = null;
            if (treeView1.SelectedNode.Parent != null)
            selectedContainer = treeView1.SelectedNode.Parent.Tag as IGUIContainer;
            if (selectedContainer == null)
                selectedContainer = treeView1.SelectedNode.Tag as IGUIContainer;
            byte X = (byte)((e.X - 4 * selectedContainer.deltaX) / 4);
            byte Y = (byte)((e.Y - 4 * selectedContainer.deltaY) / 4);
            IDCSGUI item = (IDCSGUI)Activator.CreateInstance("DCS7Design", "DCS7GUI." + listBox1.SelectedItem.ToString()).Unwrap();
            item.X = X;
            item.Y = Y;
            item.Parent = selectedContainer;
            selectedContainer.Controls.Add(item);
            Render();
            UpdateTree();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;
            IDCSGUI gui = treeView1.SelectedNode.Tag as IDCSGUI;
            int x = gui.X * 4, y = gui.Y * 4;
            if (gui.Parent != null)
            {
                x += (gui.Parent.deltaX * 4);
                y += (gui.Parent.deltaY * 4);
            }
            Pen pen = new Pen(Brushes.Red, 4);
            e.Graphics.DrawRectangle(pen, new Rectangle(x, y,
                gui.Width * 4, gui.Height * 4));
        }

        int deltaX, deltaY;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (treeView1.SelectedNode == null || e.Button != System.Windows.Forms.MouseButtons.Left)
                return;
            IDCSGUI gui = treeView1.SelectedNode.Tag as IDCSGUI;
            int x = gui.X * 4, y = gui.Y * 4;
            if (gui.Parent != null)
            {
                x += (gui.Parent.deltaX * 4);
                y += (gui.Parent.deltaY * 4);
            }
            if (Rectangle.Intersect(new Rectangle(x, y,
                gui.Width * 4, gui.Height * 4), new Rectangle(e.X, e.Y, 1, 1)) != Rectangle.Empty)
            {
                deltaX = e.X - deltaX;
                deltaY = e.Y - deltaY;
            }
            else
            {
                deltaX = -1;
                deltaY = -1;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            deltaX = -1;
            deltaY = -1;
            Render();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (treeView1.SelectedNode == null || (deltaX == -1))
                return;
            IDCSGUI gui = treeView1.SelectedNode.Tag as IDCSGUI;
            gui.X = (byte)((e.X - deltaX) / 4);
            gui.Y = (byte)((e.Y - deltaY) / 4);
            this.Invalidate(true);
        }
    }
}
